package com.system.utils.httpexcuteutils;
import com.system.po.Pinke;
import com.system.service.PinkeService;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
/**
 * @author zchome
 */
public class Goget {
	private static Logger myLog = Logger.getLogger(Goget.class);
	@Resource(name = "pinkeServiceImpl")
	private static PinkeService pinkeService;
	public static void getAll() {
		List<Pinke> pinkesList = pinkeService.selectAll();
		System.out.println(Arrays.toString(pinkesList.toArray()));
	}
	/**
	 * httpCharge
	 */
	public static Integer httpCharge(String opFare, String transactionId, String opTime, String userNo) {
		try {
			CloseableHttpClient httpCilent = HttpClients.createDefault();
			String result;
			String host = "";
			if(!StringUtils.isEmpty(host)){
				String newUrl = "http://" + host + "/custom/chargeCard.do" +//
						"?appId=" +//
						"&mchId=" +//
						"&openId=" +//
						"&userId=" +//
						"&cid=" +//
						"&userNo=" + userNo +//
						"&opFare=" + opFare +//
						"&transactionId=" + transactionId + "&opTime=" + URLEncoder.encode(opTime, "utf-8") + "&payopenId=&chargeWithType=0&userPayStatus=1&refundFlag=1";
				System.out.println("newUrl1:" + newUrl);
				//设置连接超时时间
				RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(10000)
						//设置请求超时时间
						.setSocketTimeout(15000)
						//默认允许自动重定向
						.setRedirectsEnabled(true).build();
				System.out.println("newUrl2:" + newUrl);
				HttpGet get = new HttpGet(newUrl);
				get.setConfig(requestConfig);
				try {
					HttpResponse response = httpCilent.execute(get);
					if (response.getStatusLine().getStatusCode() == 200) {
						result = EntityUtils.toString(response.getEntity());
						System.out.println("result:" + result);
						return 1;
					}
					else {
						return 5;
					}
				}
				catch (Exception e) {
					myLog.error(e.getMessage());
					e.printStackTrace();
					return 5;
				}
				finally {
					try {
						httpCilent.close();
					}
					catch (IOException e) {
						e.printStackTrace();
					}
				}
			}else{
				System.out.println("uri 不合法！");
				return 5;
			}

		}
		catch (Exception e) {
			e.printStackTrace();
			return 5;
		}
	}
}
